package top.lywivan.mm.service.imp;

import top.lywivan.mm.dao.QuestionDao;
import top.lywivan.mm.dao.ReviewDao;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.pojo.Question;
import top.lywivan.mm.pojo.ReviewLog;
import top.lywivan.mm.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private ReviewDao reviewDao;

    @Override
    @Transactional(readOnly = true)
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Map queryParams = queryPageBean.getQueryParams();
        Question question = new Question();
        if(queryParams!=null&&queryParams.size()!=0){
            question.setCourseId((Integer) queryParams.get("courseId"));
            question.setStatus((Integer) queryParams.get("status"));
            question.setDifficulty((Integer) queryParams.get("difficulty"));
            question.setType((Integer) queryParams.get("type"));
            if (queryParams.get("keyWord") != null) {
                question.setSubject("%" + queryParams.get("keyWord") + "%");
            }
        }
        Long count = questionDao.findTotalCount(question);
        List<Question> list = questionDao.findListByPage(queryPageBean.getOffset(), pageSize, question);
        return new PageResult(count, list);
    }

    @Override
    @Transactional
    public void addQuestion(Question question) {
        questionDao.addQuestion(question);
        System.out.println(question);
        ReviewLog reviewLog = new ReviewLog();
        reviewLog.setStatus(0);
        reviewLog.setQuestionId(question.getId());
        reviewDao.addJingXuan(reviewLog);
    }

    @Override
    @Transactional
    public void updateQuestion(Question question) {
        questionDao.updateQuestion(question);
    }

    @Override
    public Question findQuestionById(Integer questionId) {
        return questionDao.findQuestionById(questionId);
    }

    @Override
    public PageResult findClassicListByPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Map queryParams = queryPageBean.getQueryParams();
        Question question = new Question();
        if(queryParams!=null&&queryParams.size()!=0){
            question.setCourseId((Integer) queryParams.get("courseId"));
            question.setStatus((Integer) queryParams.get("status"));
            question.setDifficulty((Integer) queryParams.get("difficulty"));
            question.setType((Integer) queryParams.get("type"));
            question.setReviewStatus((Integer) queryParams.get("reviewStatus"));
            if (queryParams.get("keyWord") != null) {
                question.setSubject("%" + queryParams.get("keyWord") + "%");
            }
        }
        Long count = questionDao.findClassicTotalCount(question);
        List<Question> list = questionDao.findClassicListByPage(queryPageBean.getOffset(), pageSize, question);
        return new PageResult(count, list);
    }

}
